package hash_map_set;

import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86187
 * Date: 2022-08-22
 * Time: 9:31
 */

public class Test {

    //key 不是整数 是引用类型或者自定义类型  此时需要将其转化为整数  如下的例子：
    public static void main(String[] args) {
        Person person1 = new Person("1234");
        Person person2 = new Person("12345");

        //返回对象的哈希值
        //在此需重写 hashCode 方法 原因见map3 01:13:00
        System.out.println(person1.hashCode());
        System.out.println(person2.hashCode());
        System.out.println("=====================");

        HashCode<Person,String> hashCode = new HashCode<>();
        hashCode.put(person1,"1234");
        hashCode.put(person2,"12345");
        System.out.println(hashCode.get(person1));
        System.out.println(hashCode.get(person2));


    }

    //测试哈希桶
    public static void main1(String[] args) {
        HashBuck hashBuck = new HashBuck();
        hashBuck.put(1,110);
        hashBuck.put(2,181);
        hashBuck.put(3,151);
        hashBuck.put(14,11);
        hashBuck.put(5,161);
        hashBuck.put(6,141);
        hashBuck.put(7,71);
        hashBuck.put(8,81);
        hashBuck.put(9,119);
        hashBuck.put(10,101);
        System.out.println(hashBuck.get(1));
        System.out.println(hashBuck.get(5));
        System.out.println("=============================");
    }
}

/**
 *  思考：key 不是整数 是引用类型或者自定义类型  如何实现 % elem.length 操作呢？？
 *          此时需要将其转化为整数  如下的例子：
 */
class Person {
    public String id;

    public Person(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "id"+id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return Objects.equals(id, person.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}